package db.relation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;

import db.BaseDAO;
import db.DAOFactory;
import db.Transaction;
import db.TransformInterface;



import model.*;
import model.relation.Operation;

/**
 * 
 * Description: 对权限级别与权限操作的关系集数据进行处理的DAO。
 * 
 * @since  2009/2/23
 * @author ashen
 * 
 */
public class OperationDAO extends BaseDAO implements TransformInterface<Operation>{
	
	public OperationDAO(){
		setTableName("userscheme.operation");
	}
	
	/**
	 * the method save of class OperationDAO
	 * 
	 * 该方法将权限操作与权限级别的关系记录存储到数据库
	 * 自动提交事务
	 * 
	 * @param operation 需要保存的Operation对象
	 * @return 返回操作成功与否
	 */
	public boolean save(Operation operation){
		return save(object2HashMap(operation));
	}
	
	/**
	 * the method save of class OperationDAO
	 * 
	 * 该方法将权限操作与权限级别的关系记录存储到数据库
	 * 
	 * @param operation 需要保存的Operation对象
	 * @param transaction 数据库操作类Transaction的对象
	 */
	public void save(Operation operation,Transaction transaction){
		save(object2HashMap(operation),transaction);
	}
	
	/**
	 * the method update of class OperationDAO 
	 * 
	 * 该方法将operation对象更新到数据库
	 * 
	 * @param operation 需要更新的Operation对象
	 * @return 返回该操作成功与否
	 */
	public boolean update(Operation operation){
		if(findById(operation.getRoleId(),operation.getType().getType())==null){
			Transaction transaction=new Transaction();
			save(operation,transaction);
			return transaction.commit();
		}else{
			return update(object2HashMap(operation),"ROLEID",operation.getRoleId(),"TYPE","'"+operation.getType().getType()+"'");
		}
	}
	
	/**
	 * the method delete of class OperationDAO
	 * 
	 * 该方法删除权限级别ID等于参数roleId的权限操作与权限级别的关系记录
	 * 
	 * @param roleId 权限级别的ID
	 * @param transaction 数据库操作类Transaction的对象
	 */
	public void delete(int roleId,Transaction transaction){
		delete("roleId",roleId,transaction);
	}
	
	/**
	 * the method findAll of class OperationDAO
	 * 
	 * 该方法返回所有权限操作与权限级别的关系记录
	 * 
	 * @return ArrayList<Operation>类型，返回权限操作与权限级别的关系记录
	 */
	public ArrayList<Operation> findAll(){
		Transaction transaction=new Transaction();
		ArrayList<Operation> operationList= list2ObjectList(super.findAllBase(transaction));
		transaction.close();
		return operationList;
	}
	
	/**
	 * the method findByPriorityId of class OperationDAO
	 * 
	 * 该方法查找与指定ID的权限级别相关的权限操作
	 * 
	 * @param id 权限级别的ID
	 * @return ArrayList<Operation>类型的结果集合
	 */
	public ArrayList<Operation> findByRoleId(int id){
		Transaction transaction=new Transaction();
		ArrayList<Operation> operationList= 
			list2ObjectList(findByProperty("roleId", id,transaction));
		transaction.close();
		return operationList;		 
	}

	/**
	 * the method findById of class OperationDAO
	 * 
	 * 该方法查找权限级别ID等于参数roleId
	 * 且资源类型等于参数type的资源权限与权限级别的关系记录
	 * 
	 * @param roleId 权限级别ID
	 * @param type 资源类型type
	 * @return 查询到的权限操作和权限级别的关系记录
	 */
	public Operation findById(int roleId,String type){
		Transaction transaction=new Transaction();
		Operation operation= 
			hashMap2Object(findByPK("roleId",roleId,"type",getFormatString(type),transaction));
		transaction.close();
		return operation;
	}
	
	
	/**
	 * the method list2ObjectList of class OperationDAO
	 * 
	 * 该方法将存有权限级别和权限操作关系信息的HashMap对象数组转换为Operation对象数组
	 * 
	 * @param hashMapList HashMap对象数组
	 * @return Operation对象数组
	 */
	public ArrayList<Operation> list2ObjectList(ArrayList<HashMap> hashMapList){
		Operation tempObject=null;
		ArrayList<Operation> resultList=new ArrayList<Operation>();
		if(hashMapList.isEmpty())
			return resultList;
		for(Iterator i=hashMapList.iterator();i.hasNext();){
			tempObject=hashMap2Object((HashMap)i.next());
			if(tempObject!=null)
				resultList.add(tempObject);
		}
		return resultList;
	}
	
	/**
	 * the method hashtable2Point of class OperationDAO
	 * 
	 * 该方法将HashMap类型的对象转换为Operation的对象，并返回Operation的对象.
	 * 
	 * @param hashMap 存储了一个权限操作与权限级别关系信息的HashMap类型的对象
	 * @return Operation的对象.
	 */
	public Operation hashMap2Object(HashMap hashMap){
		if(hashMap==null)
			return null;
		Operation operation=new Operation();
		operation.setRoleId((Integer)hashMap.get("ROLEID"));
		ResourceType type=DAOFactory.getResourceTypeDAO().findByType((String)hashMap.get("TYPE"));
		operation.setType(type);
		operation.setPost((Integer)hashMap.get("POSTACTION"));
		operation.setPut((Integer)hashMap.get("PUTACTION"));
		operation.setDelete((Integer)hashMap.get("DELETEACTION"));
		operation.setGet((Integer)hashMap.get("GETACTION"));
		return operation;
	}
	
	/**
	 * the method object2HashMap of class OperationDAO
	 * 
	 * 该方法将参数PASet对象转换为存储参数PASet对象信息的HashMap对象，并返回
	 * 
	 * @param paset 权限操作与权限级别关系PASet对象
	 * @return 存储参数权限操作PASet对象信息的HashMap对象
	 */
	public HashMap object2HashMap(Operation operation){
		HashMap hashMap=new HashMap();
		hashMap.put("ROLEID", operation.getRoleId());
		hashMap.put("TYPE",getFormatString(operation.getType().getType()));
		hashMap.put("POSTACTION",operation.getPost());
		hashMap.put("GETACTION",operation.getGet() );
		hashMap.put("PUTACTION",operation.getPut());
		hashMap.put("DELETEACTION",operation.getDelete());
		return hashMap;
	}	
	
}